<template>
    <div class="app-container">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="项目名称" prop="xiangmumingcheng">
          <el-input
            v-model="queryParams.xiangmumingcheng"
            placeholder="请输入项目名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="科研类别" prop="keyanleibie">
          <el-input
            v-model="queryParams.keyanleibie"
            placeholder="请输入科研类别"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="科研账号" prop="keyanzhanghao" v-show="this.$storage.get('role')==='管理员'">
          <el-input
            v-model="queryParams.keyanzhanghao"
            placeholder="请输入科研账号"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="负责人" prop="fuzeren">
          <el-input
            v-model="queryParams.fuzeren"
            placeholder="请输入负责人"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <!-- <el-form-item label="学号" prop="xuehao">
          <el-input
            v-model="queryParams.xuehao"
            placeholder="请输入学号"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="学生姓名" prop="xueshengxingming">
          <el-input
            v-model="queryParams.xueshengxingming"
            placeholder="请输入学生姓名"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item> -->
        <!-- <el-form-item label="专业" prop="zhuanye">
          <el-input
            v-model="queryParams.zhuanye"
            placeholder="请输入专业"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item> -->
        <!-- <el-form-item label="是否审核" prop="sfsh">
          <el-input
            v-model="queryParams.sfsh"
            placeholder="请输入是否审核"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item> -->
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
  
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5"  v-if="this.$storage.get('role') === '科研部门'">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['system:xiangmucanyu:add']"
          >新增成员</el-button>
        </el-col>
        <!-- <el-col :span="1.5">
          <el-button
            type="success"
            plain
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['system:xiangmucanyu:edit']"
          >修改</el-button>
        </el-col> -->
        <el-col :span="1.5"  v-if="this.$storage.get('role') === '科研部门'">
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['system:xiangmucanyu:remove']"
          >删除</el-button>
        </el-col>
        <!-- <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['system:xiangmucanyu:export']"
          >导出</el-button>
        </el-col> -->
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>
  
      <el-table v-loading="loading" :data="xiangmucanyuList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <!-- <el-table-column label="主键" align="center" prop="id" /> -->
        <el-table-column label="项目名称" align="center" prop="xiangmumingcheng" />
        <!-- <el-table-column label="项目图片" align="center" prop="xiangmutupian" /> -->
        <el-table-column label="科研类别" align="center" prop="keyanleibie" />
        <el-table-column label="科研账号" align="center" prop="keyanzhanghao" />
        <el-table-column label="负责人" align="center" prop="fuzeren" />
        <!-- <el-table-column label="参与原因" align="center" prop="canyuyuanyin" /> -->
        <!-- <el-table-column label="申请时间" align="center" prop="shenqingshijian" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.shenqingshijian, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column> -->
        <el-table-column label="学生学号" align="center" prop="xuehao" />
        <el-table-column label="学生姓名" align="center" prop="xueshengxingming" />
        <el-table-column label="专业" align="center" prop="zhuanye" />
        <!-- <el-table-column label="是否审核" align="center" prop="sfsh" /> -->
        <!-- <el-table-column label="审核回复" align="center" prop="shhf" /> -->
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width"  v-if="this.$storage.get('role') === '科研部门'">
          <template slot-scope="scope">
            <!-- <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['system:xiangmucanyu:edit']"
            >修改</el-button> -->
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['system:xiangmucanyu:remove']"
              
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      
      <el-pagination
      v-show="total>0"
        background
        :total="this.total"
        :page-size="this.queryParams.limit"
        @current-change="handleCurrentChange">
      </el-pagination>
  
      <!-- 添加或修改项目参与对话框 -->
      <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body @close="handleDialogClose">
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <!-- <el-form-item label="项目名称" prop="xiangmumingcheng">
            <el-input v-model="form.xiangmumingcheng" placeholder="请输入项目名称" />
          </el-form-item> -->
          <!-- <el-form-item label="项目名称" prop="xiangmumingcheng">
          <el-radio-group v-model="form.item">
            <el-radio
              v-for="item in keyanxiangmuList"
              :key="item.id"  
              :label="item"
            >{{ item.xiangmumingcheng }}</el-radio>
          </el-radio-group>
        </el-form-item> -->
        <el-form-item label="项目名称" prop="xiangmumingcheng">
            <el-select v-model="selectedItemId" placeholder="请选择项目名称" @change="handleItemChange">
              <el-option
                v-for="item in keyanxiangmuList"
                :key="item.id"
                :label="item.xiangmumingcheng"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
          <!-- <el-form-item label="项目图片" prop="xiangmutupian">
            <el-input v-model="form.xiangmutupian" type="textarea" placeholder="请输入内容" />
          </el-form-item> -->
          <!-- <el-form-item label="科研类别" prop="keyanleibie">
            <el-input v-model="form.keyanleibie" placeholder="请输入科研类别" />
          </el-form-item> -->
          <!-- <el-form-item label="科研账号" prop="keyanzhanghao">
            <el-input v-model="form.keyanzhanghao" placeholder="请输入科研账号" />
          </el-form-item> -->
          <!-- <el-form-item label="负责人" prop="fuzeren">
            <el-input v-model="form.fuzeren" placeholder="请输入负责人" />
          </el-form-item>
          <el-form-item label="参与原因" prop="canyuyuanyin">
            <el-input v-model="form.canyuyuanyin" placeholder="请输入参与原因" />
          </el-form-item> -->
          <!-- <el-form-item label="申请时间" prop="shenqingshijian">
            <el-date-picker clearable
              v-model="form.shenqingshijian"
              type="date"
              value-format="yyyy-MM-dd"
              placeholder="请选择申请时间">
            </el-date-picker>
          </el-form-item> -->
          <el-form-item label="学号" prop="xuehao">
            <el-input v-model="form.xuehao" placeholder="请输入学号" />
          </el-form-item>
          <!-- <el-form-item label="学生姓名" prop="xueshengxingming">
            <el-input v-model="form.xueshengxingming" placeholder="请输入学生姓名" />
          </el-form-item> -->
          <!-- <el-form-item label="专业" prop="zhuanye">
            <el-input v-model="form.zhuanye" placeholder="请输入专业" />
          </el-form-item> -->
          <!-- <el-form-item label="是否审核" prop="sfsh">
            <el-input v-model="form.sfsh" placeholder="请输入是否审核" />
          </el-form-item> -->
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>
    </div>
  </template>
  
  <script>
//   import { listXiangmucanyu, getXiangmucanyu, delXiangmucanyu, addXiangmucanyu, updateXiangmucanyu } from "@/api/system/xiangmucanyu";
  
  export default {
    name: "Xiangmucanyu",
    data() {
      return {
        // 用于存储选中的项目 ID
        selectedItemId: null, 
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 项目参与表格数据
        xiangmucanyuList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 科研项目表格数据
        keyanxiangmuList: [],
        // 查询参数
        queryParams: {
            page: 1,
            limit: 10,
          xiangmumingcheng: null,
          keyanleibie: null,
          keyanzhanghao: null,
          fuzeren: null,
          xuehao: null,
          xueshengxingming: null,
          zhuanye: null,
          sfsh: null,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          addtime: [
            { required: true, message: "创建时间不能为空", trigger: "blur" }
          ],
          xiangmutupian: [
            { required: true, message: "项目图片不能为空", trigger: "blur" }
          ],
        }
      };
    },
    created() {
      this.getList();
      this.getxiangmucanyuList();
    },
    methods: {
      handleDialogClose(){
        this.selectedItemId=null;
      },
      handleCurrentChange(val){
        this.queryParams.page = val;
        this.getList();
      },
      getxiangmucanyuList() {
        const role = this.$storage.get('role');
        const adminName = this.$storage.get('adminName'); // 提前获取 adminName，避免重复调用

        if (role === '科研部门') {
            this.queryParams.keyanzhanghao = adminName;
        }
        this.$http({
    url: `/keyanxiangmu/list`,
    method: "get",
    params: this.queryParams // 使用 params 而不是 data
}).then(response => {
          this.keyanxiangmuList = response.data.data.list;
          // this.total = response.data.data.total;
          // this.loading = false;
        });
      },
      /** 查询项目参与列表 */
      getList() {
        this.loading = true;
        const role = this.$storage.get('role');
        const adminName = this.$storage.get('adminName'); // 提前获取 adminName，避免重复调用

        if (role === '科研部门') {
            this.queryParams.keyanzhanghao = adminName;
        } else if (role === '学生') {
            this.queryParams.xuehao = adminName;
        }
        this.$http({
    url: `/xiangmucanyu/list`,
    method: "get",
    params: this.queryParams // 使用 params 而不是 data
}).then(response => {
          this.xiangmucanyuList = response.data.data.list;
          this.total = response.data.data.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.selectedItemId=null;
        this.resetForm();
      },
      // 表单重置
      resetForm() {
        this.form = {
          id: null,
          addtime: null,
          xiangmumingcheng: null,
          xiangmutupian: null,
          keyanleibie: null,
          keyanzhanghao: null,
          fuzeren: null,
          canyuyuanyin: null,
          shenqingshijian: null,
          xuehao: null,
          xueshengxingming: null,
          zhuanye: null,
          sfsh: null,
          shhf: null
        };
        // this.reset("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.queryParams.xiangmumingcheng=null;
        this.queryParams.keyanleibie=null;
        this.queryParams.fuzeren=null;
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length!==1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.resetForm();
        this.open = true;
        this.title = "添加项目参与";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        getXiangmucanyu(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改项目参与";
        });
      },
      handleItemChange(value) {
          // 根据选中的 ID 查找对应的项目对象，并赋值给 form.item
          const selectedItem = this.keyanxiangmuList.find(item => item.id === value);
          if (selectedItem) {
            this.form.item = selectedItem;
          } else {
            this.form.item = null; // 如果没有选中任何项目，则设置为 null
          }
        },
      /** 提交按钮 */
      submitForm() {
        this.form.keyanleibie = this.form.item.keyanleibie;
        this.form.keyanzhanghao = this.form.item.keyanzhanghao;
        this.form.fuzeren = this.form.item.fuzeren;
        this.form.xiangmumingcheng = this.form.item.xiangmumingcheng;
        this.$http({
    url: `/xiangmucanyu/add`,
    method: "post",
    data: this.form // 使用 params 而不是 data
}).then(valid => {
  if (valid.data.code != 0) {
      alert(valid.data.msg);
      this.open = false;
      this.getList();
  }
  this.open = false;
  this.getList();
          // if (valid) {
          //   if (this.form.id != null) {
          //     updateXiangmucanyu(this.form).then(response => {
          //       this.$modal.msgSuccess("修改成功");
          //       this.open = false;
          //       this.getList();
          //     });
          //   } else {
          //     addXiangmucanyu(this.form).then(response => {
          //       this.$modal.msgSuccess("新增成功");
          //       this.open = false;
          //       this.getList();
          //     });
          //   }
          // }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id ? [row.id] : this.ids;
        this.$confirm('是否确认删除科研项目编号为"' + ids + '"的数据项？').then(() => {
        // 用户点击了确定按钮，执行删除操作
        this.$http({
              url: `/xiangmucanyu/delete`,
              method: "post",
              data: ids
          }).then(response => {
              this.getList();
          });
      }).catch(() => {
        // 用户点击了取消按钮，执行其他操作或什么都不做
        this.ids = [];
        console.log('用户取消了删除操作');
      });
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('system/xiangmucanyu/export', {
          ...this.queryParams
        }, `xiangmucanyu_${new Date().getTime()}.xlsx`)
      }
    }
  };
  </script>
  